# All the spells for the warhammer Mod
# Made by Lord Olleus
#Last edited 30th June 06

from CvPythonExtensions import *
import CvUtil
import sdToolKit
import CvMagicUtils
import time 

# globals
gc = CyGlobalContext()
sd = sdToolKit


# Return 1 to show animation
# Return 0 to do nothing

class Spells:

	def FlamingSword (self, pSpellUnit):
		pCaster = pSpellUnit.getTransportUnit()
		pCaster.setHasPromotion(gc.getInfoTypeForString("PROMOTION_FLAMING_SWORD"), 1)
		pCaster.changeMoves(60)
		if pSpellUnit.getOwner() == 0:
			CyInterface().addMessage(0, False, 5, CyTranslator(). getText("TXT_KEY_FLAMING_SWORD_CAST", () ), "", 0, gc.getUnitInfo(pSpellUnit.getUnitType()).getButton(), CyGame().getReplayMessageColor(20), pSpellUnit.getX(), pSpellUnit.getY(), True, False)		
		
		
class RangeSpells:
		
	def BurningHead(self, iX, iY, pSpellUnit):
		pCaster = pSpellUnit.getTransportUnit()
		pPlot = pCaster.plot()
		pSpellPlot = gc.getMap().plot(iX, iY)
		jX = pPlot.getX()
		jY = pPlot.getY()
		PathList = CvMagicUtils.PathList(iX, iY, jX, jY)
		bHumanHurt = False
		pCaster.changeMoves(60)
		
		for pLoopPlot in PathList:
			for i in range(0, pLoopPlot.getNumUnits() ):
				pDefender = pLoopPlot.getUnit(i)
				if pDefender.getOwner() == 0:
					bHumanHurt = True
				if not(CvMagicUtils.isEnemy(pSpellUnit.getOwner(), pDefender.getOwner() )):
					return
				pDefender.changeDamage(10, -1)
		
		if pSpellUnit.getOwner() == 0:
			CyInterface().addMessage(0, False, 5, CyTranslator(). getText("TXT_KEY_BURNING_HEAD_CAST_GOOD", () ), "", 0, gc.getUnitInfo(pSpellUnit.getUnitType()).getButton(), CyGame().getReplayMessageColor(20), iX, iY, True, False)
		if bHumanHurt == True:
			CyInterface().addMessage(0, False, 5, CyTranslator(). getText("TXT_KEY_BURNING_HEAD_CAST_BAD", () ), "", 0, gc.getUnitInfo(pSpellUnit.getUnitType()).getButton(), CyGame().getReplayMessageColor(20), iX, iY, True, True)
		pCaster.changeMoves(60)


	def ConflagrationOfDoom(self, iX, iY, pSpellUnit):
		pCaster = pSpellUnit.getTransportUnit()
		pSpellPlot = gc.getMap().plot(iX, iY)
		bHumanHurt = False

		for i in range( 0, pSpellPlot.getNumUnits() ):
			pDefender = pSpellPlot.getUnit(i)
			if pDefender.getOwner() == 0:
				bHumanHurt = True
			if not(CvMagicUtils.isEnemy(pSpellUnit.getOwner(), pDefender.getOwner() )):
				return
			iDamage = gc.getGame().getSorenRand().get(25, "Conflagration of Doom Damage")
			pDefender.changeDamage(iDamage, -1)
			pDefender.setHasPromotion(gc.getInfoTypeForString("PROMOTION_CONFLAGRATION_FIRE"), True)
			
		if pSpellUnit.getOwner() == 0:
			CyInterface().addMessage(0, False, 5, CyTranslator(). getText("TXT_KEY_CONFLAGRATION_OF_DOOM_CAST_GOOD", () ), "", 0, gc.getUnitInfo(pSpellUnit.getUnitType()).getButton(), CyGame().getReplayMessageColor(20), iX, iY, True, False)
		if bHumanHurt == True:
			CyInterface().addMessage(0, False, 5, CyTranslator(). getText("TXT_KEY_CONFLAGRATION_OF_DOOM_CAST_BAD", () ), "", 0, gc.getUnitInfo(pSpellUnit.getUnitType()).getButton(), CyGame().getReplayMessageColor(20), iX, iY, True, True)
		pCaster.changeMoves(60)
		# Check every unit at the begining of every turn to see if it is on 'fire'

		
	def WallOfFire (self, iX, iY, pSpellUnit):
		pCaster = pSpellUnit.getTransportUnit()
		pSpellPlot = gc.getMap().plot(iX, iY)
		iNumUnits = pSpellPlot.getNumUnits()
		jX = pCaster.plot().getX()
		jY = pCaster.plot().getY()
		pDefender = pCaster #uused just before spawning unit in stupid check
		bHumanHurt = False

		if iNumUnits != 0:
			for i in range( 0, iNumUnits ):
				pDefender = pSpellPlot.getUnit(i)
				if pDefender.getOwner() == 0:
					bHumanHurt = True
				if not(CvMagicUtils.isEnemy(pSpellUnit.getOwner(), pDefender.getOwner() )):
					return
				iDamage = gc.getGame().getSorenRand().get(20, "Wall Of Fire")
				pDefender.changeDamage(iDamage, -1)
			if iX > jX:
				iX -= 1
			elif iX < jX:
				iX += 1

			if iY > jY:
				iY -= 1
			elif iY < jY:
				iY += 1
				
		pSpawnPlot = gc.getMap().plot(iX, iY)
		if pSpawnPlot != pCaster.plot() and (pSpawnPlot.getNumDefenders(pDefender.getOwner()) == 0 or pDefender == pCaster):
			(gc.getPlayer(pCaster.getOwner())).initUnit(gc.getInfoTypeForString("UNIT_FIREWALL"), iX, iY,UnitAITypes.NO_UNITAI) 
		# Remove this unit at the begining of the next turn.

		if pSpellUnit.getOwner() == 0:
			CyInterface().addMessage(0, False, 5, CyTranslator(). getText("TXT_KEY_WALL_OF_FIRE_CAST_GOOD", () ), "", 0, gc.getUnitInfo(pSpellUnit.getUnitType()).getButton(), CyGame().getReplayMessageColor(20), iX, iY, True, False)
		if bHumanHurt == True:
			CyInterface().addMessage(0, False, 5, CyTranslator(). getText("TXT_KEY_WALL_OF_FIRE_CAST_BAD", () ), "", 0, gc.getUnitInfo(pSpellUnit.getUnitType()).getButton(), CyGame().getReplayMessageColor(20), iX, iY, True, True)
		pCaster.changeMoves(60)


	def TransmutationOfLead (self, iX, iY, pSpellUnit):
		pCaster = pSpellUnit.getTransportUnit()
		pSpellPlot = gc.getMap().plot(iX, iY)
		bHumanHurt = False
		for i in range(pSpellPlot.getNumUnits()):
			pDefender = pSpellPlot.getUnit(i)
			if pDefender.getOwner() == 0:
				bHumanHurt = True
			if not(CvMagicUtils.isEnemy(pSpellUnit.getOwner(), pDefender.getOwner() )):
				return
			pDefender.changeDamage(10, -1)
			pDefender.setHasPromotion(gc.getInfoTypeForString("PROMOTION_NO_FIRST_STRIKES"), true)

		if pSpellUnit.getOwner() == 0:
			CyInterface().addMessage(0, False, 5, CyTranslator(). getText("TXT_KEY_TRANSMUTATION_OF_LEAD_CAST_GOOD", () ), "", 0, gc.getUnitInfo(pSpellUnit.getUnitType()).getButton(), CyGame().getReplayMessageColor(20), iX, iY, True, False)
		if bHumanHurt == True:
			CyInterface().addMessage(0, False, 5, CyTranslator(). getText("TXT_KEY_TRANSMUTATION_OF_LEAD_CAST_BAD", () ), "", 0, gc.getUnitInfo(pSpellUnit.getUnitType()).getButton(), CyGame().getReplayMessageColor(20), iX, iY, True, True)
		pCaster.changeMoves(60)
		return


class UnitSelectSpells:

	def FireBall (self, pSpellUnit, pDefender):
		pCaster = pSpellUnit.getTransportUnit()
		if not(CvMagicUtils.isEnemy(pSpellUnit.getOwner(), pDefender.getOwner())):
				return
		pDefender.changeDamage(20, -1)
		
		if pSpellUnit.getOwner() == 0:
			CyInterface().addMessage(0, False, 5, CyTranslator(). getText("TXT_KEY_FIREBALL_CAST_GOOD", () ), "", 0, gc.getUnitInfo(pSpellUnit.getUnitType()).getButton(), CyGame().getReplayMessageColor(20), pDefender.getX(), pDefender.getY(), True, False)
		if pDefender.getOwner() == 0:
			CyInterface().addMessage(0, False, 5, CyTranslator(). getText("TXT_KEY_FIREBALL_CAST_BAD", () ), "", 0, gc.getUnitInfo(pSpellUnit.getUnitType()).getButton(), CyGame().getReplayMessageColor(20), pDefender.getX(), pDefender.getY(), True, True)		
		pCaster.changeMoves(60)
	
	
	def FieryBlast(self, pSpellUnit, pDefender):
		pCaster = pSpellUnit.getTransportUnit()
		pSpellPlot = pDefender.plot()
		pDefender.changeDamage(20, -1)
		bHumanHurt = False
		
		for i in range( 0, pSpellPlot.getNumUnits() ):
			pDefender = pSpellPlot.getUnit(i)
			if pDefender.getOwner() == 0:
				bHumanHurt = True
			if not(CvMagicUtils.isEnemy(pSpellUnit.getOwner(), pDefender.getOwner() )):
				return
			pDefender.changeDamage(10, -1)
			
		if pSpellUnit.getOwner() == 0:
			CyInterface().addMessage(0, False, 5, CyTranslator(). getText("TXT_KEY_FIERY_BLAST_CAST_GOOD", () ), "", 0, gc.getUnitInfo(pSpellUnit.getUnitType()).getButton(), CyGame().getReplayMessageColor(20), pDefender.getX(), pDefender.getY(), True, False)
		if bHumanHurt == 0:
			CyInterface().addMessage(0, False, 5, CyTranslator(). getText("TXT_KEY_FIERY_BLAST_CAST_BAD", () ), "", 0, gc.getUnitInfo(pSpellUnit.getUnitType()).getButton(), CyGame().getReplayMessageColor(20), pDefender.getX(), pDefender.getY(), True, True)	
		pCaster.changeMoves(60)
	
	
	def BurningIron  (self, pSpellUnit, pTargetUnit):
		pSpellPlot = pTargetUnit.plot()
		pCaster = pSpellUnit.getTransportUnit()
		
		if not(CvMagicUtils.isEnemy(pSpellUnit.getOwner(), pTargetUnit.getOwner() )):
			return
		else:
			iDamage = gc.getGame().getSorenRand().get(15, "Burning Iron")
			pTargetUnit.changeDamage(iDamage + 15, -1)
			
		if pSpellUnit.getOwner() == 0:
			CyInterface().addMessage(0, False, 5, CyTranslator(). getText("TXT_KEY_BURNING_IRON_CAST_GOOD", () ), "", 0, gc.getUnitInfo(pSpellUnit.getUnitType()).getButton(), CyGame().getReplayMessageColor(20), pTargetUnit.getX(), pTargetUnit.getY(), True, False)
		if pTargetUnit.getOwner() == 0:
			CyInterface().addMessage(0, False, 5, CyTranslator(). getText("TXT_KEY_BURNING_IRON_CAST_BAD", () ), "", 0, gc.getUnitInfo(pSpellUnit.getUnitType()).getButton(), CyGame().getReplayMessageColor(20), pTargetUnit.getX(), pTargetUnit.getY(), True, True)	
		pCaster.changeMoves(60)
		
		
	# Works on UNITCOMBAT_SIEGE and _CHARIOTS
	def CommandmentOfBrass (self, pSpellUnit, pTargetUnit):
		pCaster = pSpellUnit.getTransportUnit()
		pSpellPlot = pTargetUnit.plot()
		if not(CvMagicUtils.isEnemy(pSpellUnit.getOwner(), pTargetUnit.getOwner() )):
			return
		if ( pTargetUnit.getUnitCombatType() == gc.getInfoTypeForString("UNITCOMBAT_SIEGE") or pTargetUnit.getUnitCombatType() == gc.getInfoTypeForString("UNITCOMBAT_CHARIOT")):
			pTargetUnit.setHasPromotion(gc.getInfoTypeForString("PROMOTION_IMOBALIZED"), true)
		
			if pSpellUnit.getOwner() == 0:
				CyInterface().addMessage(0, False, 5, CyTranslator(). getText("TXT_KEY_COMMANDMENT_OF_BRASS_CAST_GOOD", () ), "", 0, gc.getUnitInfo(pSpellUnit.getUnitType()).getButton(), CyGame().getReplayMessageColor(20), pTargetUnit.getX(), pTargetUnit.getY(), True, False)
			if pTargetUnit.getOwner() == 0:
				CyInterface().addMessage(0, False, 5, CyTranslator(). getText("TXT_KEY_COMMANDMENT_OF_BRASSCAST_BAD", () ), "", 0, gc.getUnitInfo(pSpellUnit.getUnitType()).getButton(), CyGame().getReplayMessageColor(20), pTargetUnit.getX(), pTargetUnit.getY(), True, True)	
			pCaster.changeMoves(60)
			return
		CyInterface().addImmediateMessage("Unit Invalid; Please select another one")
		
		


		
